package org.cloud.provider.controller;

import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor;
import org.cloud.provider.config.TopicRabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hjx
 * @title: SendMessageController
 * @description: TODO
 * @date 2021/10/28
 */
@RestController
@AllArgsConstructor
public class SendMessageController {

    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendTopicMessage")
    public String sendDirectMessage() {
        String messageId = IdUtil.randomUUID();
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPIC_EXCHANGE, "topic.message", manMap);
        return "ok";
    }
    @GetMapping("/sendTopicMessage2")
    public String sendDirectMessage2() {
        String messageId = IdUtil.randomUUID();
        String messageData = "message:  woman ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPIC_EXCHANGE, "topic.notice", manMap);
        return "ok";
    }

}
